<?php
	/**
	 * User: Hunman
	 * Date: 18.06.13
	 * Time: 22:07
	 */
$timeStartAllPage = microtime();
	//подключение автозагрузчика класов
	require_once( "loader.php" );
	//определяем по урлу какой клас вызывать
	try
	{
		//получаем урл
		$requestURI = strtolower ( parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

		//формируем имя контроллера
		$url    = explode('/', $requestURI);
		$url[1] = ucfirst($url[1]);
		$path   = $url[1] == null ? 'Controller_Splash' : "Controller_$url[1]";

		$controllerFolderList = array();

		if ( $controllerFolderList == null && $handle = opendir($_SERVER["DOCUMENT_ROOT"] . '/Controller') )
		{
			while ( false !== ( $entry = readdir($handle) ) )
			{
				$controllerFolderList[] = $entry;
			}
		}

		$url[1] .= $url[1] == null ? 'Splash.php' : '.php';
		if ( array_search($url[1], $controllerFolderList) )
		{
            if ($url[3] == null)
            {
                new $path($url[2]);
            } else
            {
                new $path($url[2], $url[3]);
            }

		}
		else
		{
			throw new Exception ( 404 );
		}


		$timeLoadAllPage = microtime() - $timeStartAllPage;
		echo ( "<br> Time to load this page is <strong>" . $timeLoadAllPage . "</strong>" );
	}

	catch ( Exception $e )
	{
		echo $e->getMessage();
		echo $e->getCode();
		echo $e->getLine();
		echo "Page not found";
		echo $e->getFile();
	}

